var cartList  = []
// 创建个数组储存是否被选中 根据local获取长度 
let checkedList = []
for (var i = 0; i < localStorage.length; i++) {
    checkedList.push('checked')
    // 1表示被选中 0表示不被选中
}
//  获取地址栏传过来的数据
let username = getQueryVariable('name')
//  右上方用户信息

cartName()
function cartName() {
    $('.cart_name').text(username)
    // 绑定退出按钮
    $('.zhuce_li').find('a').on('click', function () {
        window.location.href = "http://www.yjf.com:8080/page/login.html "
    })
    // 返回首页
    $('.head-bottom').on('click','img',function(){
        window.location.href = "http://www.yjf.com:8080/page/index.html?name="+username
    })
}
// 获取购物车数据
function obtainCartList() {
    cartList = []
    for (var i = 0; i < localStorage.length; i++) {
        var key = localStorage.key(i)
        cartList.push(JSON.parse(localStorage[key]))
    }
}
// 渲染购物车
bindCart()
function bindCart() {
    obtainCartList()
    let num = 0
    let price = 0
    // 判断购物车里是否有数据
    if (!cartList.length) {
        $('.container-hide').css('display', 'none')
        $('.container-show').css('display', 'block')
    }else if(cartList.length){
        $('.container-hide').css('display', 'block')
        $('.container-show').css('display', 'none')
    }
    cartList.forEach(e => {
        let unitPrice = e.deal_global ? e.deal_global.price_home : e.deal_group.price_dis
        price += (e.report_number + 1) * unitPrice
        num += parseInt(e.report_number + 1)
    })
    str = template('tmp', {
        list: cartList,
        num: num,
        price: price.toFixed(2),
        ischecked: checkedList

    })
          
    $('.center_top').html(str)
     // 渲染完页面后初始化全选按钮
    qx()  
}
// 加减的点击事件
btnclick()
function btnclick() {
    $('.center_top ').on('click', 'button', function (e) {
        let target = e.target
        // 获取索引对应的localStorage key值
        let key = localStorage.key($(this).parent().parent().index())
        // 接收对应的localStorage
        let tem = JSON.parse(localStorage[key])

        if (target.className == 'num-' && tem.report_number > 0) {
            tem.report_number--
        }
        else if (target.className == 'num+' && tem.report_number < 9) {
            tem.report_number++
        }
        // 将localStorage里面的数据转换出来  属性report_number 减一 然后覆盖原值
        window.localStorage.setItem(key, JSON.stringify(tem))
        // 删除按钮
        if (target.className == 'deal_del') {
           checkedList.splice($(this).parent().parent().index(),1)
            window.localStorage.removeItem(key)
        }
        // 重新渲染购物车
        bindCart()

    })

}

// 获取渲染每日推荐数据
obtainDaylist()
function obtainDaylist() {
    $.ajax({
        url: '/jm/GetHomeTodayLists/100/201',
        type: 'GET',
        dataType: "json",
        success: function (res) {
            let list = res.list
            bindDay(list)
            // 添加购物车事件
            evenAddTo(list)
            // 添加去往详情页事件
            goDetails('.recommend_center',list)
        }
    })

}
//渲染每日推荐
function bindDay(list) {
    let listDay = list.slice(0,10)
    var str = template('tmptwo', {
        list: listDay
    })
    $('.recommend_center').html(str)
}


function qx(){
    let xz = $('input:checkbox:checked').filter('.danxuan').length
    // 获取总的多选按钮数量
    let allcheck = $("input[type='checkbox']").filter('.danxuan').length
    // 比较
    if (xz < allcheck) {
        $('.quanxuan').prop("checked", false)
    } else if (allcheck == xz) {
        $('.quanxuan').prop("checked", true)
    }
}

quanXuan()
function quanXuan() {

    $('.center_top').on('click', 'input', function (e) {
        let target = e.target
        if (target.className == 'danxuan') {
            let index = $(this).parent().parent().parent().index()
            checkedList[index] = ''
            // 初始化全选按钮
            qx()
        }
        // 全选按钮
        if (target.className == 'quanxuan') {
            let check = this.checked?'checked':''
            checkedList = checkedList.map(function(){
                return check
            })
            $("input[type='checkbox']").filter('.danxuan').prop("checked", this.checked)
        }
    })
    $('.center_top').on('click', 'a', function (e) {
        // 继续购物
        let target = e.target
        if (target.className == 'go_shopping') {
            window.location.href = "http://www.yjf.com:8080/page/index.html?name=" + username
        }
        // 清空选中商品
        if (target.className == 'clear_shopping') {
            let key = []
            ;[...$('input:checkbox:checked').filter('.danxuan')].forEach(e => {
                // 获取选中的索引
                let index = $(e).parent().parent().parent().index()
                
                //获取索引对应的local 的key值
                key.push(localStorage.key(index))
            })
            // 删除对应的key
            key.forEach(e=>{
                window.localStorage.removeItem(e)
            })
            bindCart()
        }
    })
}

// 每日推荐 添加购物车绑定事件
function evenAddTo(list){
    $('.recommend_center').on('click', 'button', function (e) {
        e = e || window.event
        // 如果没登录,跳转到登录页面
        e.stopPropagation();
        if(username == false){
            window.location.href="http://www.yjf.com:8080/page/login.html"
        }
        // 点击商品的index
        let index = $(this).parent().parent().index()
        let frg = false
        for (var i = 0; i < localStorage.length; i++) {
            if (index == localStorage.key(i)) {
                frg = true
            }
        }
        if (frg == true) {
            let zancun = JSON.parse(localStorage[index])
            // 数量+1
            zancun.report_number++
            // 再将数据封装到local里面
            localStorage.setItem(index, JSON.stringify(zancun));
        } else if (frg == false) {
            localStorage.setItem(index, JSON.stringify(list[index]));
            checkedList.push('checked')
        }
        // 事件结束后渲染购物车
        bindCart()
        // 点击动画效果
        donghua(e.pageX,e.pageY)
    })
}


//返回商店
$('.show_right').on('click','button',function(){
    window.location.href = "http://www.yjf.com:8080/page/index.html?name="+username
    return false
})
